﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.Utils;
using WebApiTest.Models;
using WebApiTest.Service.Basic;

namespace WebApiTest.Controllers.basic
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeptController : ControllerBase
    {
        private DeptService deptService = new DeptService();

        [HttpPost("query")]
        public TableRspDTO list4Table(TableReqDTO tableReqDTO)
        {
            TableRspDTO tableRspDTO = new TableRspDTO();
            try
            {
                tableRspDTO = deptService.list4Table(tableReqDTO);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return tableRspDTO;
        }

        [HttpPost("add")]
        public OpResultDTO add(Dept dept)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = deptService.add(dept).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("update")]
        public OpResultDTO update(Dept dept)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = deptService.update(dept).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("remove")]
        public OpResultDTO remove(JObject request)
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.message = deptService.remove(request["deptIds"].ToString()).ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }

        [HttpPost("listAll")]
        public OpResultDTO listAll()
        {
            OpResultDTO opResult = new OpResultDTO();
            try
            {
                opResult.result = deptService.listAll();
                opResult.message = "success";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                opResult.message = "error";
            }
            return opResult;
        }
    }
}
